<?php 
class Jssdk {
        var $appid;
        var $appsecret; 
        var $WX;
        var $timestamp;
	public function __construct( $appid, $appsecret )
	{
                include_once  'Wechat_lib.php';
		$options=array(
			'token'=>'yuwencheck',
			'encodingaeskey'=>'AToc8cQgfq2F5OPqDn1fM9ddjCk8opwTlNa8pGzR1TN',
			'appid'=>$appid,
			'appsecret'=>$appsecret
			
		);
                
                $this->appid = $appid;
                $this->appsecret = $appsecret;
                $this->timestamp = time();
                $this->WX = new Wechat_lib( $options );
	}

	public function index()
	{
		$jsTicket = $this->WX->getJsTicket();
                $api_ticket = $this->WX->get_apiTicket();
		if ( !$jsTicket ) {
                    $data=array(
                        'code'=>400,
                        'msg'=>'获取js_ticket失败！错误码：'.$this->WX->errCode
                    );
                    
                    $this->echoJson($data);
		}
                
                $url = $_GET['url'];
		$noncestr = $this->WX->generateNonceStr();
		$jsSign = $this->WX->getJsSign( $url, $this->timestamp, $noncestr );
		$signature = $jsSign['signature'];
                $debug = ( $_GET['debug'] == 'true' ) ? true : false;
		$data = array(
                    'debug' => $debug,
                    'appId' => $this->appid, // 必填，公众号的唯一标识
                    'timestamp' => $this->timestamp, // 必填，生成签名的时间戳
                    'nonceStr' => $noncestr, // 必填，生成签名的随机串
                    'signature' => $signature, // 必填，签名，见附录1
                    'jsApiList' => array(
                            'checkJsApi',
                            'onMenuShareTimeline',
                            'onMenuShareAppMessage',
                            'onMenuShareQQ',
                            'onMenuShareWeibo',
                            'hideMenuItems',
                            'showMenuItems',
                            'hideAllNonBaseMenuItem',
                            'showAllNonBaseMenuItem',
                            'translateVoice',
                            'startRecord',
                            'stopRecord',
                            'onVoiceRecordEnd',
                            'playVoice',
                            'onVoicePlayEnd',
                            'pauseVoice',
                            'stopVoice',
                            'uploadVoice',
                            'downloadVoice',
                            'onRecordEnd',
                            'chooseImage',
                            'previewImage',
                            'uploadImage',
                            'downloadImage',
                            'getNetworkType',
                            'openLocation',
                            'getLocation',
                            'hideOptionMenu',
                            'showOptionMenu',
                            'closeWindow',
                            'scanQRCode',
                            'chooseWXPay',
                            'openProductSpecificView',
                            'addCard',
                            'chooseCard',
                            'openCard'
                    )
		);
	
            $this->echoJson( $data );
	}
        
        public function card()
	{
            $jsSign = $this->WX->getJsSign2( $this->card_id, $this->timestamp, $this->appid );
            $signature = $jsSign['signature'];
            
            $data = array(
                    'cardId' => $this->card_id,
                    'timestamp' => $jsSign['timestamp'], // 必填，生成签名的时间戳
                    'api_ticket' => $jsSign['api_ticket'], // 必填，生成签名的时间戳
                    'signature' => $signature // 必填，签名，见附录1
            ); 
            
            $this->echoJson( $data );
        }
        
        function getCard(){
            $info = $this->WX->getCardInfo( $this->card_id );
            $this->echoJson( $info );
        }

	
	public function echoJson($data){
		$callback = $_GET['callback'];
		$json = json_encode($data);
		echo $callback.'('.$json.')';
		exit();
	}

}
$act = $_REQUEST['act'];
if( $act == "" ) $act = "index222";

if( $act == "index" ){
    $class = new Jssdk( $_GET['appid'], $_GET['appsecret'] );
    $class->index();
}elseif( $act == "card" ){
    $class = new Jssdk();
    $class->card();
}elseif( $act == "getCard" ){
    $class = new Jssdk();
    $class->getCard();
}elseif( $act == "lzcq" ){//请求参数
    $class = new Jssdk( "", "" );//(appid, 秘钥)
    $class->index();
}


/* End of file jssdk.php */
/* Location: ./application/controllers/jssdk.php */